package Game;
import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;

import org.newdawn.slick.geom.Circle;

import org.newdawn.slick.geom.Shape;
import org.newdawn.slick.geom.Vector2f;

//TODO : Float or int? 
//TODO : Better movements - use angle and math package?


public class Enemy{

	// The x coordinate for tile the enemy is on.
	private float x;
	// The y coordinate for tile the enemy is on
	private float y;
	// The image for the enemy
	private Image image;
	//
	private Circle body;
	// The position which the enemy is heading in
	private Path destination;
	// The movement speed of the enemy
	private float SPEED = 0.001f;
	//The direction of the enemy. 
	private float direction;
	// Specifies the position on a tile.
	float t;
	//The health of the enemy.
	private int health;
	// The maximum health of the enemy
	private final static int MAX_HEALTH = 30;
	// The enemy's health bar
	private Graphics healthbar;
	
	private float toX;
	private float toY;
	
	/**
	 * Constructor for Enemy objects
	 * @param startingPath the starting position of the enemy
	 * @throws SlickException
	 */
	public Enemy(Path startingPath) throws SlickException{
		image = new Image("/data/enemy.gif");
		if(startingPath == null){
			//DO SOMETHING
		}
		x = Map.TILESIZE*startingPath.getX();
		y = Map.TILESIZE*startingPath.getY();
		destination = startingPath;
		direction = destination.getAngle();
		image.setRotation(destination.getAngle());
		health =  MAX_HEALTH;
		body = new Circle(x - Map.TILESIZE/2, y - Map.TILESIZE/2, Map.TILESIZE);
		toX = destination.getX()*Map.TILESIZE;
		toY = destination.getY()*Map.TILESIZE;
		
		healthbar = new Graphics();
		healthbar.setColor(new Color(Color.red));
		
	}
	
	/**
	 * Moves the Enemy a small step forward, depending
	 * on speed, update speed and direction. Returns false
	 * if the Enemy has reached the end of the path, else true
	 * @param delta 
	 * @return true if its at the end of the path, else false
	 */
	public boolean move(int delta) {
		int dX = destination.getX()*Map.TILESIZE;
		int dY = destination.getY()*Map.TILESIZE;
		//Check if we've roughly arrived at the location
		if(Math.abs(dX - x) < 1 && Math.abs(dY - y) < 1){
			//Rotate image
			direction = destination.getAngle();
			x = dX;
			y = dY;
			//Get the new destination
			destination = destination.getNext();
			
			//If there is none - we've reached the end.
			if(destination == null){			
				return false;
			}
			//Else, get the next destination coordinates
			
			dX = destination.getX()*Map.TILESIZE;
			dY = destination.getY()*Map.TILESIZE;
		}
		
		/* USED FOR SMOOTH ROTATION
		if(angle != destination.getAngle()){
			angle += (destination.getAngle() - angle)*SPEED*delta*1.2;
			image.setRotation(angle);
		}*/
		
		
		
		//The amount of distance traveled during this move.
		float dist = SPEED*delta*100;
		if(x == dX){
			if(y < dY)
				y += dist;
			else
				y -= dist;
		} else if(y == dY){
			if(x < dX)
				x += dist;
			else
				x -= dist;
		} 
		body = new Circle(x - Map.TILESIZE/2, y - Map.TILESIZE/2, Map.TILESIZE);
		return true;
	}
	
	/**
	 * Draw this enemy on the screen.
	 */
	public void draw() {
		image.setRotation(direction);
		image.draw(x, y);
		drawHealthBar();
		
	}
	
	/**
	 * Draw this enemy's health bar on the screen
	 */
	public void drawHealthBar()
	{
		Float ratio = (float)health/ (float)MAX_HEALTH;
		healthbar.fillRect(x, y, ratio*50 , 10);
	}

	
	public boolean move(int delta, int hej){
		float dX = toX - x;
		float dY = toY - y;
		if(Math.abs(dX) < 1 && Math.abs(dY) < 1){
			direction = destination.getAngle();
			x = toX;
			y = toY;
			destination = destination.getNext();
			if(destination == null)
				return false;
			t = SPEED*delta;
			toX = destination.getX()*Map.TILESIZE;
			toY = destination.getY()*Map.TILESIZE;
		} 
		t += SPEED * delta;
		x += t*dX;
		y += t*dY;
		body = new Circle(x - Map.TILESIZE/2, y, Map.TILESIZE);
		return true;
	}
	
	/**
	 * Return a point on the line between two
	 * given points based on a given time.
	 * @param a the starting point
	 * @param b the destination point
	 * @param t the time passed
	 * @return a point between a and b
	 */
	public float lerp(float a, float b, float t){
		if(t < 0){
			return a;
		}
		return a + t*(b - a);
	}
	
	/**
	 * Returns true if this enemy is dead,
	 * else false
	 * @return true if dead, else false.
	 */
	public boolean isDead() {
		return health <= 0 ? true : false;
	}

	public Shape getTargetArea(){
		return body;
	}
	
	public void takeDamage(int damage){
		health -= damage;
	}
	
	
	
}
